﻿using UnityEngine;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace Room
{
    public class RCUIUnionRevivePoint : RCUIBase
    {
        UISlider slider;
        GameObject progressBar;
        UI.UnionFightReviveData script;
        public void SetCampCount(int camp0,int camp1)
        {
            var charactor = owner.charactorManager.GetSelfCharactor();
            int friend = charactor.Camp == 0 ? camp0 : camp1;
            int enemy = charactor.Camp == 1 ? camp0 : camp1;
            script.friendCount.text = friend + "";
            script.enemyCount.text = enemy + "";
        }

        public void SetSlider(float campSlider0,float campSlider1)
        {
            var charactor = owner.charactorManager.GetSelfCharactor();
            slider.value = charactor.Camp == 0 ? campSlider0 : campSlider1;
            if(slider.value > 0 && slider.value < 1)
                SetBarShow(true);
        }

        public RCUIUnionRevivePoint(RoomCharactor owner) : base(owner)
        {
            type = UIType.UnionRevivePoint;
        }

        public void Init()
        {
            if(progressBar == null)
            {
                InitProgressBar();
            }
        }

        void InitProgressBar()
        {
            var prefab = Resources.Load<GameObject>("Prefab/ReviceProgressBar");
            progressBar = GameObject.Instantiate<GameObject>(prefab);
            slider = progressBar.GetComponent<UISlider>();
            progressBar.transform.SetParent(uiObj.transform);
            progressBar.transform.localPosition = Vector3.zero;
            progressBar.transform.localScale = Vector3.one;
            script = progressBar.GetComponent<UI.UnionFightReviveData>();
            SetBarShow(false);
        }

        public void SetBarShow(bool value)
        {
            progressBar.SetActive(value);
        }

        float showTime;
        bool isShow;
        public void ShowOccupyEffect(int camp)
        {
            var charactor = owner.charactorManager.GetSelfCharactor();
            bool isFriend = charactor.Camp == camp;
            script.friendOccupy.SetActive(isFriend);
            script.enemyOccupy.SetActive(!isFriend);
            isShow = true;
            showTime = 1f;
        }

        void HideEffect()
        {
            script.friendOccupy.SetActive(false);
            script.enemyOccupy.SetActive(false);
            SetBarShow(false);
        }

        public override void Tick()
        {
            showTime -= Time.deltaTime;
            if(showTime <= 0 && isShow)
            {
                HideEffect();
                isShow = false;
            }
        }
    }
}

